#pragma once

#include <string>
#include <memory>
#include <nlohmann/json.hpp>
#include <functional>
#include <unordered_map>

class GameServer;

class IProtocolHandler {
public:
    virtual ~IProtocolHandler() = default;
    virtual bool validate(GameServer& game_server, const nlohmann::json& message, uint32_t session_id) = 0;
    virtual void process(GameServer& game_server, const nlohmann::json& message, uint32_t session_id) = 0;
};

class MessageHandler {
public:
    /**
     * @brief 注册协议
     * @param message_type 消息类型
     * @param protocol 协议实例
     */
    void register_protocol(const std::string& message_type, std::shared_ptr<IProtocolHandler> protocol);

    /**
     * @brief 处理消息
     * @param game_server 游戏服务器
     * @param message 消息
     * @param session_id 会话ID
     */
    void handle_message(GameServer& game_server, const nlohmann::json& message, uint32_t session_id);
private:
    
    std::unordered_map<std::string, std::shared_ptr<IProtocolHandler>> protocols_;
};